﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace PerformanceAssessment.Models
{
    public class UserModel
    {
        public UserType UserType { get; set; }
        public int ID { get; set; }

        public UserModel() { }
        public UserModel(UserType t)
        {
            UserType = t;
        }

        public static UserModel GetCurrentUser()
        {
            MembershipUser user = Membership.GetUser();
            using (PerformanceAssessmentDB dbEmployee = new PerformanceAssessmentDB())
            {
                if (Roles.IsUserInRole("Administrator"))
                {
                    return new UserModel(UserType.Administrator);
                }
                else
                {
                    //It checks first for employee type
                    UserModel retUser;
                    Employee emp =
                      (from u in dbEmployee.Employees
                       where u.Email.Trim().ToLower() == user.Email.Trim().ToLower()
                       select u).FirstOrDefault();
                    if (emp != null)
                    {
                        retUser = emp;
                        retUser.UserType = UserType.Employee;
                    }
                    else
                    {
                        Manager man = (from u in dbEmployee.Managers
                                       where u.Email.Trim().ToLower() == user.Email.Trim().ToLower()
                                       select u).FirstOrDefault();
                        if (man != null)
                        {
                            retUser = man;
                            retUser.UserType = UserType.Manager;
                        }
                        else
                        {
                            retUser = new UserModel(UserType.None);
                        }
                    }
                    return retUser;
                }
            }
        }
    }
}